<?php

/**
 * This is the model class for table "xyh_news".
 *
 * The followings are the available columns in table 'xyh_news':
 * @property string $id
 * @property string $alumni_id
 * @property string $title
 * @property string $banner_url
 * @property string $min_banner_url
 * @property string $content
 * @property string $ctime
 * @property string $mtime
 * @property string $dtime
 * @property string $status
 * @property string $is_del
 * @property string $zan_count
 * @property string $fav_count
 */
class News extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
    
    
        //public  $is_Hot=2;
        public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xyh_news';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('content', 'required','message'=>'请填写内容！'),
			//array('alumni_id, ctime, mtime, dtime, zan_count, fav_count', 'length', 'max'=>10),
			array('title, banner_url, min_banner_url', 'length', 'max'=>255),
			array('status, is_del', 'length', 'max'=>1),
                        array('ctime','safe'),
                        array('id','safe'),
                        array('is_hot','numerical'),
                        array('alumni_id','numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, alumni_id, title, banner_url, min_banner_url, content, ctime, mtime, dtime, status, is_del, zan_count, fav_count', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
                     'Alumni' => array(self::BELONGS_TO, 'Alumni', 'alumni_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'alumni_id' => 'Alumni',
			'title' => 'Title',
			'banner_url' => 'Banner Url',
			'min_banner_url' => 'Min Banner Url',
			'content' => 'Content',
			'ctime' => 'Ctime',
			'mtime' => 'Mtime',
			'dtime' => 'Dtime',
			'status' => 'Status',
			'is_del' => 'Is Del',
			'zan_count' => 'Zan Count',
			'fav_count' => 'Fav Count',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($AlumniId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

               
                 if($AlumniId){
                    $sqlAlumni = "SELECT a.id FROM xyh_alumni as a WHERE a.parent_id={$AlumniId}";
                    $dataArr = Yii::app()->db->createCommand($sqlAlumni)->queryAll();
                    
                    $addC ="alumni_id =$AlumniId";
                    foreach ($dataArr as $v){
                        $addC .= " or alumni_id =$v[id] ";
                    }
                    //p_e($addC);
                    $criteria->addCondition($addC);
                }
                
              if($this->ctime){
                    $tmp = strtotime($this->ctime);
                    $criteria->addCondition("ctime >=$tmp");
                }
                
                
                $criteria->addCondition('is_del=2');
                 $criteria->compare('t.id', $this->id, true);
	
		$criteria->compare('alumni_id',$this->alumni_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('banner_url',$this->banner_url,true);
		$criteria->compare('min_banner_url',$this->min_banner_url,true);
		$criteria->compare('content',$this->content,true);
		//$criteria->compare('ctime',$this->ctime,true);
		$criteria->compare('mtime',$this->mtime,true);
		$criteria->compare('dtime',$this->dtime,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('is_del',$this->is_del,true);
		$criteria->compare('zan_count',$this->zan_count,true);
		$criteria->compare('fav_count',$this->fav_count,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'sort'=>array('defaultOrder'=>'id DESC'),         
                        'pagination' => array(
                            'pageSize' => 10, //代表每页显示2条信息 
                        ),
       
		));
	}
        
        
        public function getBackground($value){
             if ($value > 0){
              echo '<span class="badge badge-important">'.$value.'</span>';          
            }else{

                echo $value;
            }
        }
        
        
        public function getHot($is_hot){
            
            echo $is_hot==1?'<p class="badge badge-important" >是</p>':'';
            
        }
        
        
       
}